home *** CD-ROM | disk | FTP | other *** search
- Path: anvil.ugrad.cs.ubc.ca!not-for-mail
- From: c2a192@ugrad.cs.ubc.ca (Kazimir Kylheku)
- Newsgroups: comp.lang.c
- Subject: Re: What is &Variable (declared as: char Variable[10])?
- Date: 27 Feb 1996 15:27:04 -0800
- Organization: Computer Science, University of B.C., Vancouver, B.C., Canada
- Message-ID: <4h0408INNe3q@anvil.ugrad.cs.ubc.ca>
- References: <4gqpa1$3h9@alcor.usc.edu> <1996Feb26.211807.28858@isac.hces.com>
- NNTP-Posting-Host: anvil.ugrad.cs.ubc.ca
-
- In article <1996Feb26.211807.28858@isac.hces.com>,
- Greg Goodrich <gg@isac.hces.com> wrote:
- >Abu Wawda (wawda@alcor.usc.edu) wrote:
- >: I'm having trouble understanding what the address of a static array
- >: is. For example, if I declare a variable called myarray as:
- >: char myarray[10];
- >: then what could &myarray possibly mean? myarray is not a pointer, so
- >: &myarray could not possibly be the address of the variable myarray
- >: (like it would be if I did char* myarray and then asked for &myarray).
- >
- >: Functions such as scanf() allow the following:
- >
- >: char myarray[10];
- >
- >: scanf("%s",&myarray);
- >
- >: but I don't understand what scanf() could possibly be taking in the
- >: second parameter. It can't be: char** since myarray is not a
- >: pointer. I CAN understand how the following would work:
- >
- >This is because C treats the occurrence of array names as the address of
- >the array. Therefore the following are equivalent:
-
- ... you mean, the address of the first element, right?
-
- >
- > scanf("%s", myarray);
- > scanf("%s", &myarray);
- >
- >The thing is, the address is implied when you use an array name. This
- >is not so for pointers. I am not sure why C was incorporated in this
- >way. In my humble opinion, it would make it easier and clearer to force
- >the programmer to put the & in front of array names, the same as you
- >have to do for any other storage class.
-
- No. The use of & in front of array names is perfectly consistent with its use
- with other objects. An & in front of a structure name gives you a value
- whose type is a pointer to that structure. An & in front of a long variable
- gives you a pointer to long. An & in front of a function name gives you a
- pointer to that function type. And an & in front of an array name gives you a
- pointer to that array (rather than a pointer to the element type).
-
- Where the treatment of arrays (and functions) differs is when you state the
- name by itself in an expression. Unfortunately, C adopted this clever little
- rule that if you name an array, the resulting expression is a pointer to the
- first element, rather than a reference to the array. If this little unfortunate
- semantic rule did not exist, the name of an array could be treated as an
- lvalue, and array assignment would be a possibility. This is one operation in
- which C gives you two ways. You can say 'array_name', or you can say
- '&array_name[0]'. However, in structures, there is no equivalence between
- 'struct_name' and '&struct_name.first_member'.
-
- The unfortunate collapsing rule is directly related to the equivalence of
- E1[E2] and *((E1) + (E2)), which requires that the E1 array designator (or
- perhaps E2) collapse into a pointer. This silly equivalence, incidentally,
- allows the expressions a[i], i[a], to be equivalent, provided a is an array
- (or pointer) and i an integral type (or vice versa :). The [] postfix operator
- could easily have been adopted as special, just like the -> or . operator, and
- distinct from pointer dereferencing. Then a restriction that the only way to
- get the pointer to element 0 of an array would be with the & operator would
- allow the same privileges for arrays that are shared by structures (passing by
- value, copy by assigning, returning from functions for compatible arrays).
- Note that [] could still be usable with pointers---it would be overloaded.
-
- That would not be the C language anymore, though. C is cool the way it is!
- --
-
-
-
- --
-
-